home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / des_c.exe / lha / BTOA.C < prev    next >
Text File  |  1990-07-14  |  3KB  |  155 lines

  1. /* btoa: version 4.0
  2.  * stream filter to change 8 bit bytes into printable ascii
  3.  * computes the number of bytes, and three kinds of simple checksums
  4.  * incoming bytes are collected into 32-bit words, then printed in base 85
  5.  *  exp(85,5) > exp(2,32)
  6.  * the ASCII characters used are between '!' and 'u'
  7.  * 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
  8.  *
  9.  *  Paul Rutter        Joe Orost
  10.  *  philabs!per        petsd!joe
  11.  *
  12.  *  WARNING: this version is not compatible with the original as sent out
  13.  *  on the net.  The original encoded from ' ' to 't'; which cause problems
  14.  *  with some mailers (stripping off trailing blanks).
  15.  */
  16.  
  17. /* DjG - Don Gloistein
  18.  *  Made some changes to compile with msdos/16 bit compilers. Some compilers
  19.  *  do not make the same assumptions on promoting an integer to long constant.
  20.  *  Msdos computers support a text and binary mode file open. Stdin/stdout are
  21.  *  defaulted to text streams. Included SETMODE for those systems. If compiled
  22.  *  without any defines, it will default to the stock unix version.
  23.  */
  24.  
  25. #include <stdio.h>
  26.  
  27. #ifdef MSC
  28. #include <fcntl.h>
  29. #include <io.h>
  30. #define SETMODE(x) setmode(fileno((x)),O_BINARY)
  31. #endif
  32.  
  33. #ifdef __TURBOC__
  34. #include <fcntl.h>
  35. #include <io.h>
  36. #define SETMODE(x) setmode(fileno((x)),O_BINARY)
  37. #endif
  38.  
  39. #ifdef MWC_ATARI
  40. #define SETMODE(x) ((x)->_ff &= ~(_FASCII))
  41. #endif
  42.  
  43. /* following take care of unix systems */
  44.  
  45. #ifndef SETMODE
  46. #define SETMODE(x)
  47. #endif
  48.  
  49. #define reg register
  50.  
  51. #define MAXPERLINE 78
  52.  
  53. long int Ceor = 0L;
  54. long int Csum = 0L;
  55. long int Crot = 0L;
  56.  
  57. long int ccount = 0L;
  58. int bcount = 0;
  59. long int word = 0L;
  60.  
  61. #define EN(c)    (int) ((c) + '!')
  62.  
  63. encode(c)
  64.   reg c;
  65. {
  66.   Ceor ^= c;
  67.   Csum += c;
  68.   Csum += 1;
  69.   if ((Crot & 0x80000000)) {
  70.     Crot <<= 1;
  71.     Crot += 1;
  72.   } else {
  73.     Crot <<= 1;
  74.   }
  75.   Crot += c;
  76.  
  77.   word <<= 8;
  78.   word |= c;
  79.   if (bcount == 3) {
  80.     wordout(word);
  81.     bcount = 0;
  82.   } else {
  83.     bcount += 1;
  84.   }
  85. }
  86.  
  87. wordout(word)
  88.   reg long int word;
  89. {
  90.   if (word == 0) {
  91.     charout('z');
  92.   } else {
  93.     reg int tmp = 0;
  94.  
  95.     if(word < 0L) {  /* Because some don't support unsigned long */
  96.       tmp = 32;
  97.       word = word - (85L * 85L * 85L * 85L * 32L);
  98.     }
  99.     if(word < 0L) {
  100.       tmp = 64;
  101.       word = word - (85L * 85L * 85L * 85L * 32L);
  102.     }
  103.     charout(EN((word / (85L * 85L * 85L * 85L)) + tmp));
  104.     word %= (85L * 85L * 85L * 85L);
  105.     charout(EN(word / (85L * 85L * 85L)));
  106.     word %= (85L * 85L * 85L);
  107.     charout(EN(word / (85L * 85L)));
  108.     word %= (85L * 85L);
  109.     charout(EN(word / 85L));
  110.     word %= 85L;
  111.     charout(EN(word));
  112.   }
  113. }
  114.  
  115. charout(c) {
  116.   putchar(c);
  117.   ccount += 1;
  118.   if (ccount == MAXPERLINE) {
  119.     putchar('\n');
  120.     ccount = 0;
  121.   }
  122. }
  123.  
  124. main(argc,argv)
  125.   char **argv;
  126. {
  127.   reg c;
  128.   reg long int n;
  129.  
  130.   if (argc != 1) {
  131.     fprintf(stderr,"bad args to %s\n", argv[0]);
  132.     exit(2);
  133.   }
  134.  
  135.   SETMODE(stdin);
  136.   SETMODE(stdout);
  137.  
  138.   printf("xbtoa Begin\n");
  139.   n = 0;
  140.   while ((c = getchar()) != EOF) {
  141.     encode(c);
  142.     n += 1;
  143.   }
  144.  
  145.   while (bcount != 0) {
  146.     encode(0);
  147.   }
  148.  
  149.   /* n is written twice as crude cross check*/
  150.  
  151.   printf("\nxbtoa End N %ld %lx E %lx S %lx R %lx\n", n, n, Ceor, Csum, Crot);
  152.  
  153.   exit(0);
  154. }
  155.